package compareResult.datasetCompare;

import com.supermap.data.CursorType;
import com.supermap.data.DatasetVector;
import com.supermap.data.FieldInfo;
import com.supermap.data.Recordset;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 矢量数据集比较器：自定义比较规则
 * Created by hanyz on 2016/11/8.
 */
public class DatasetVectorComparer extends DatasetComparer {
	//默认忽略系统字段。组件版本变动可能导致系统字段不一致。需要比较时设置为false，如网络数据集
	public boolean isIgnoreSYSFields = true;
	public String[] orderByField;//排序字段
	public String queryParam;//查询条件
	public int[] IDs;//比较固定记录


	public boolean compare(DatasetVector expDataset, DatasetVector actualDataset) throws Exception {
		super.compareCommon(expDataset, actualDataset);
		//对象个数
		if (expDataset.getRecordCount() != actualDataset.getRecordCount()) {
			compareError += "expDataset recordCount:" + expDataset.getRecordCount() + "\nactualDataset recordCount:" + actualDataset.getRecordCount();
			throw new Exception(compareError);
		}
		//字符集
		if (!expDataset.getCharset().equals(actualDataset.getCharset())) {
			compareError += "expDataset charset:" + expDataset.getCharset().toString() + "\nactualDataset charset:" + actualDataset.getCharset().toString();
			throw new Exception(compareError);
		}
		//空间索引
		if (!expDataset.getSpatialIndexType().equals(actualDataset.getSpatialIndexType())) {
			compareError += "expDataset SpatialIndexType:" + expDataset.getSpatialIndexType().toString() + "\nactualDataset SpatialIndexType:" + actualDataset.getSpatialIndexType().toString();
			throw new Exception(compareError);
		}
		//容限
		try {
			CompareUtilities.compareDatasetTolerance(expDataset.getTolerance(), actualDataset.getTolerance());
		} catch (Exception e) {
			compareError += e.getMessage();
			throw new Exception(compareError);
		}
		Recordset expRecordset = null;
		Recordset actualRecordset = null;
		try {
			//比较属性表结构
			expRecordset = expDataset.getRecordset(true, CursorType.STATIC);//获取空的记录集即可
			actualRecordset = actualDataset.getRecordset(true, CursorType.STATIC);
			ArrayList<FieldInfo> expFieldInfos;
			ArrayList<FieldInfo> actualFieldInfos;
			if (isIgnoreSYSFields) {
				expFieldInfos = CompareUtilities.getNSYField(expRecordset);
				actualFieldInfos = CompareUtilities.getNSYField(actualRecordset);
			} else {
				expFieldInfos = CompareUtilities.getFields(expRecordset);
				actualFieldInfos = CompareUtilities.getFields(actualRecordset);
			}
			CompareUtilities.compareFieldInfos(expFieldInfos, actualFieldInfos);
			//根据comparer（this）条件获取记录集，比较详细信息
			expRecordset = getRecordset(expDataset);
			actualRecordset = getRecordset(actualDataset);
			CompareUtilities.compareRecordset(expRecordset, actualRecordset, expFieldInfos, tolerance);
		} catch (Exception e) {
			compareError += e.getMessage();
			throw new Exception(compareError);
		} finally {
			if (expRecordset != null) {
				expRecordset.dispose();
			}
			if (actualRecordset != null) {
				actualRecordset.dispose();
			}
		}
		return true;
	}

	/**
	 * 根据comparer属性条件，获取记录集，用来比较记录集详细信息。
	 * 可以根据 orderbyField queryParam IDs来查询记录集
	 *
	 * @return
	 */
	public Recordset getRecordset(DatasetVector dataset) {
		//根据IDs获取记录集
		if (IDs != null && IDs.length > 0) {
			return dataset.query(IDs, CursorType.STATIC);
		}
		int recordCount = dataset.getRecordCount();
		//没有设置任何筛选条件：记录集在100条内时，取全部。
		if (recordCount <= 100) {
			return dataset.getRecordset(false, CursorType.STATIC);
		}
		//没有设置任何筛选条件：记录集在100条内时，取全部。
		if (recordCount <= 100) {
			return dataset.getRecordset(false, CursorType.STATIC);
		}
		//没有设置任何筛选条件：记录集大于100条时，随机取5条。
		Random random = new Random();
		List<Integer> integerList = new ArrayList<Integer>();
		for (int i = 0; i < 5; i++) {
			int randomInt = random.nextInt(recordCount + 1);
			if (!integerList.contains(randomInt)) {
				integerList.add(randomInt);
			}
		}
		IDs = new int[integerList.size()];
		for (int i = 0; i < integerList.size(); i++) {
			IDs[i] = integerList.get(i);
		}
		return dataset.query(IDs, CursorType.STATIC);
	}
}

